// 新增批量结算 耿琰 20220422
import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Input from '@antd/form/input'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Title = $Lazy.load(()=>import('#tp/title/title-small'))
const Button = $Lazy.load(()=>import('@antd/button'))
const Table = $Lazy.load(()=>import('@antd/table'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		selectedKeys:[],
		selectedKeys2: [],
		selectedRows: [],
		rows:[],
		forms : [
			{ label: '物价代码',		name: 'price_code',	width:200 },
			{ label: '项目名称',		name: 'item_name',	width:200 },
		]
	}
	
	model = {}
	query = {}
	clearKeys = false
	
	componentDidMount(){
		
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'qt-item-hospital/index')
	// 打开弹窗并传递参数
	open = ({hosp_id}) => {
		this.modal.open()
		this.query = {hosp_id}
	}
	
	// 表单
	cols = [
		{ title: '物价代码', 	dataIndex: 'price_code' },
		{ title: '项目名称', 	dataIndex: 'item_name' },
		{ title: '标准价', 		dataIndex: 'price' },
		{ title: '结算价', 		dataIndex: 'contract_price' },
		{ title: '折扣率', 		dataIndex: 'discount_rate' },
		TableControls.controls.call(this, { width: 80 })
	]
	table_controls = (text,rows) => {
		return [
			rows.qt_price_audit_uuid?TableControls.click.call(this, '撤回',()=>{
				$http.submit(this,'qt-hospital-price-audit/del',{param:{uuids:[rows.qt_price_audit_uuid]}}).then(()=>{
					$fn.fetch.call(this,'qt-item-hospital/index')
				})
			} ):null,
		]
	}
	cols2 = [
		{ title: '物价代码', 	dataIndex: 'price_code' },
		{ title: '项目名称', 	dataIndex: 'item_name' },
		{ title: '标准价', 		dataIndex: 'price' },
		{ title: '结算价', 		dataIndex: 'contract_price',
			render:(text,row)=>{
				return <Input
					value = {text}
					onChange = { value=>{
						const { rows } = this.state
						rows.forEach((v,i)=>{
							if(v.uuid === row.uuid){
								rows[i]['contract_price'] = Number(value).toFixed(2)
								rows[i]['discount_rate'] = ((Number(value).toFixed(2))/row.price).toFixed(3)
							}
						})
						this.setState({rows})
					} }
					
				/>
			}
		},
		{ title: '折扣率', 		dataIndex: 'discount_rate' },
		{ title: '操作', 		width:60, render: (text, {uuid}) => <Button label='删除' onClick={()=>this.onDel([uuid])} /> },
	]
	
	controls = ()=>{
		const { selectedKeys2 } = this.state
		return [
			Controls.click.call(this,'删除', () => this.onDel(selectedKeys2), {disabled: !$fn.hasArray(selectedKeys2)})
		]
	}
	
	// 删除
	onDel = delKeys => {
		let { rows } = this.state
		
		rows.forEach( (v,i) =>{
			delKeys.forEach( m => {
				if(v.uuid === m){
					rows[i] = null
				}
			} )
		})
		rows = rows.filter(v => v !== null)
		
		const keys = rows.map( v => v.uuid)
		
		this.setState({ selectedRows : rows, selectedKeys: keys,  selectedKeys2: [], rows })
	}
	
	render(){
		const { selectedKeys2, pullLoading, look, rows } = this.state
		const TableComponent = (
			<>
				<Title title='待修改' className='mb10 tbor1' controls={this.controls()} />
				<Table 
					data 			= { rows }
					cols 			= { this.cols2}
					loading			= { pullLoading }
					wrapClassName 	= 'mlr'
					selectedKeys	= { selectedKeys2 }
					type			= { 1 }
					simple
					onSelect		= { selectedKeys => this.setState({ selectedKeys2: selectedKeys })}
				/>
			</>
		)
		return (
			<>
				{
					TableModal.call(this,{ title: '批量修改', width:'60%', height: look ? '70%' : '99%', noRefresh:true,
						bodyStyle		: {padding:'0 10px 0'},
						table			: { 
										onSelect: (selectedKeys, selectedRows) => {
											const rows = $fn.copy(selectedRows)
											this.setState({ rows })
										}
						},
						close			: ()=> this.setState({ selectedKeys:[], selectedRows:[],  selectedKeys2: [], rows: [] }),
						suffix			: TableComponent,
						api				: 'qt-item-hospital/batchEdit',
						getSubmit		: () =>{
							const params = rows.map(v=>({
								hosp_id:v.hosp_id,
								uuid:v.uuid,
								price:v.contract_price,
								discount_rate:v.discount_rate,
								enabled:v.enabled,
								source_price:v.price,
								item_name:v.item_name,
								price_code:v.price_code,
								price_rel_codes:v.price_rel_codes
							}))
							return {params}
						},
						hasOkDisabled	: false
					})
				}
			</>
		)
	}
}

export default Index